import {Action, ActionTree, Commit} from "vuex";
import {AddToCartPayload, Product, State} from "@/store/index";
import * as types from './mutation-type'
// 加入购物车, 为什么不放在cart模块中，估计还是因为需要操作全局state的product列表
const addToCart: Action<State, any> = (context: { commit: Commit }, product: Product) => {
    if (product.inventory > 0) {
        const payload: AddToCartPayload = {
            id: product.id
        };
        context.commit(types.ADD_TO_CART, payload)
        context.commit(types.DECREMENT_PRODUCT_INVENTORY, payload)
    }
};

const actions: ActionTree<State, any> = {
    addToCart
};

export default actions
